package com.jxdinfo.hussar.example.tsgl.book.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jxdinfo.hussar.common.tfmodel.TfModelEnum;
import com.jxdinfo.hussar.example.tsgl.book.dao.BookMapper;
import com.jxdinfo.hussar.example.tsgl.book.dto.BookBookdataset1;
import com.jxdinfo.hussar.example.tsgl.book.model.Book;
import com.jxdinfo.hussar.example.tsgl.book.service.BookService;
import com.jxdinfo.hussar.example.tsgl.book.vo.BookPageVO;
import com.jxdinfo.hussar.platform.core.base.apiresult.ApiResponse;
import com.jxdinfo.hussar.support.datasource.annotations.HussarDs;
import com.jxdinfo.hussar.support.exception.HussarException;
import com.jxdinfo.hussar.support.mp.base.query.generator.SingleTableQueryGenerator;
import com.jxdinfo.hussar.support.mp.base.service.impl.HussarServiceImpl;
import com.jxdinfo.hussar.support.transaction.core.annotation.HussarTransactional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 图书表服务实现类
 */
@Service("tsgl.book.BookServiceImpl")
@HussarDs("master")
public class BookServiceImpl extends HussarServiceImpl<BookMapper, Book> implements BookService {

    private static final Logger logger = LoggerFactory.getLogger(BookServiceImpl.class);

    private static final String RETURN_CODE = "0";

    @Autowired
    private BookMapper bookMapper;

    @Override
    public ApiResponse<Book> formQuery(String id) {
        try {
            return ApiResponse.success(this.getById(id));
        } catch (Exception e) {
            throw new HussarException(TfModelEnum.TF_FORM_QUERY_FAILED.getMessage());
        }
    }

    @Override
    @HussarTransactional
    public ApiResponse<String> insertOrUpdate(Book book) {
        try {
            this.saveOrUpdate(book);
            return ApiResponse.success(String.valueOf(book.getId()), "");
        } catch (Exception e) {
            throw new HussarException(TfModelEnum.TF_FAILED_T0_ADD_OR_MODIFY.getMessage());
        }
    }

    @Override
    public ApiResponse<BookPageVO> hussarQueryPage(Page<Book> page) {
        try {
            Page<Book> pager = new Page<>(page.getCurrent(), page.getSize());
            Map<String, String[]> parameterMap = new HashMap<>();
            String[] orderParameter = new String[1];
            if (page.orders() != null) {
                for (OrderItem orderItem : page.orders()) {
                    if (orderItem.isAsc()) {
                        orderParameter[0] = orderItem.getColumn() + ",asc;";
                    } else {
                        orderParameter[0] = orderItem.getColumn() + ",desc;";
                    }
                }
            }
            parameterMap.put("orderRule", orderParameter);
            BookPageVO bookPageVO = new BookPageVO();
            // 查询条件构造器,将排序条件封装成QueryWrapper
            SingleTableQueryGenerator singleTableQueryGenerator = new SingleTableQueryGenerator();
            QueryWrapper<Book> queryWrapper = singleTableQueryGenerator.initQueryWrapper(new Book(), parameterMap);
            List<Book> list = this.page(pager, queryWrapper).getRecords();
            bookPageVO.setData(list);
            bookPageVO.setCount(pager.getTotal());
            bookPageVO.setCode(RETURN_CODE);
            return ApiResponse.success(bookPageVO);
        } catch (Exception e) {
            throw new HussarException(TfModelEnum.TF_FORM_QUERY_FAILED.getMessage());
        }
    }

    @Override
    public ApiResponse<BookPageVO> hussarQueryPage_order_custom(Page<Book> page) {
        try {
            Page<Book> pager = new Page<>(page.getCurrent(), page.getSize());
            Map<String, String[]> parameterMap = new HashMap<>();
            String[] orderParameter = new String[1];
            if (page.orders() != null) {
                for (OrderItem orderItem : page.orders()) {
                    if (orderItem.isAsc()) {
                        orderParameter[0] = orderItem.getColumn() + ",asc;";
                    } else {
                        orderParameter[0] = orderItem.getColumn() + ",desc;";
                    }
                }
            }
            parameterMap.put("orderRule", orderParameter);
            BookPageVO bookPageVO = new BookPageVO();
            // 查询条件构造器,将排序条件封装成QueryWrapper
            SingleTableQueryGenerator singleTableQueryGenerator = new SingleTableQueryGenerator();
            QueryWrapper<Book> queryWrapper = singleTableQueryGenerator.initQueryWrapper(new Book(), parameterMap);
            List<Book> list = this.page(pager, queryWrapper).getRecords();
            bookPageVO.setData(list);
            bookPageVO.setCount(pager.getTotal());
            bookPageVO.setCode(RETURN_CODE);
            return ApiResponse.success(bookPageVO);
        } catch (Exception e) {
            throw new HussarException(TfModelEnum.TF_FORM_QUERY_FAILED.getMessage());
        }
    }

    @Override
    @HussarTransactional
    public ApiResponse<Boolean> del(List<String> ids) {
        try {
            return ApiResponse.success(this.removeByIds(ids));
        } catch (Exception e) {
            throw new HussarException(TfModelEnum.TF_DELETION_FAILED.getMessage());
        }
    }

    @Override
    public ApiResponse<BookPageVO> hussarQuerybookCondition_1Page(BookBookdataset1 bookdataset1) {
        try {
            BookPageVO bookPageVO = new BookPageVO();
            Page<Book> pager = new Page<>(bookdataset1.getCurrent(), bookdataset1.getSize());
            Map<String, String[]> parameterMap = new HashMap<>();
            String[] orderParameter = new String[1];
            if (bookdataset1.getOrders() != null) {
                for (OrderItem orderItem : bookdataset1.getOrders()) {
                    if (orderItem.isAsc()) {
                        orderParameter[0] = orderItem.getColumn() + ",asc;";
                    } else {
                        orderParameter[0] = orderItem.getColumn() + ",desc;";
                    }
                }
            }
            parameterMap.put("orderRule", orderParameter);
            bookdataset1.setBooknameFullLike(bookdataset1.getBooknameFullLike().replaceAll("/", "//").replaceAll("%", "/%").replaceAll("_", "/_").replaceAll("\\[", "/[").replaceAll("]", "/]"));
            // 查询条件构造器,将排序条件封装成QueryWrapper
            SingleTableQueryGenerator singleTableQueryGenerator = new SingleTableQueryGenerator();
            QueryWrapper<Book> queryWrapper = singleTableQueryGenerator.initQueryWrapper(new Book(), parameterMap);
            List<Book> list = bookMapper.hussarQuerybookCondition_1Page(pager, bookdataset1, queryWrapper);
            bookPageVO.setData(list);
            bookPageVO.setCount(pager.getTotal());
            bookPageVO.setCode(RETURN_CODE);
            return ApiResponse.success(bookPageVO);
        } catch (Exception e) {
            throw new HussarException(TfModelEnum.TF_FORM_QUERY_FAILED.getMessage());
        }
    }

    @Override
    public ApiResponse<BookPageVO> hussarQuerybookCondition_1Page_order_custom(BookBookdataset1 bookdataset1) {
        try {
            BookPageVO bookPageVO = new BookPageVO();
            Page<Book> pager = new Page<>(bookdataset1.getCurrent(), bookdataset1.getSize());
            Map<String, String[]> parameterMap = new HashMap<>();
            String[] orderParameter = new String[1];
            if (bookdataset1.getOrders() != null) {
                for (OrderItem orderItem : bookdataset1.getOrders()) {
                    if (orderItem.isAsc()) {
                        orderParameter[0] = orderItem.getColumn() + ",asc;";
                    } else {
                        orderParameter[0] = orderItem.getColumn() + ",desc;";
                    }
                }
            }
            parameterMap.put("orderRule", orderParameter);
            bookdataset1.setBooknameFullLike(bookdataset1.getBooknameFullLike().replaceAll("/", "//").replaceAll("%", "/%").replaceAll("_", "/_").replaceAll("\\[", "/[").replaceAll("]", "/]"));
            // 查询条件构造器,将排序条件封装成QueryWrapper
            SingleTableQueryGenerator singleTableQueryGenerator = new SingleTableQueryGenerator();
            QueryWrapper<Book> queryWrapper = singleTableQueryGenerator.initQueryWrapper(new Book(), parameterMap);
            List<Book> list = bookMapper.hussarQuerybookCondition_1Page_order_custom(pager, bookdataset1, queryWrapper);
            bookPageVO.setData(list);
            bookPageVO.setCount(pager.getTotal());
            bookPageVO.setCode(RETURN_CODE);
            return ApiResponse.success(bookPageVO);
        } catch (Exception e) {
            throw new HussarException(TfModelEnum.TF_FORM_QUERY_FAILED.getMessage());
        }
    }

    @Override
    public ApiResponse<BookPageVO> hussarQuerybookCondition_1(BookBookdataset1 bookdataset1) {
        return null;
    }
}
